package com.web.portal.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.portal.pojo.IFile;
import com.web.portal.response.ResponseResult;
import com.web.portal.services.IFileService;
import com.web.portal.utils.DateUtils;
import com.web.portal.utils.file.UploadImageBean;
import com.web.portal.utils.file.UploadImageResult;
import com.web.portal.utils.file.UploadImageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2021-12-04
 */
@Slf4j
@Api(tags = "文件控制器")
@RestController
@RequestMapping("/system/sysFile")
public class SysFileController {
    @Autowired
    IFileService iFileService;

    @GetMapping("/selectData")
    @CrossOrigin
    public List<IFile> selectData(HttpServletResponse response){
            response.setHeader("Access-Control-Allow-Origin", "*");
        return iFileService.list();
    }

    @GetMapping("/selectDataById/{id}")
    @CrossOrigin
    public IFile selectDataById(@PathVariable int id, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        return iFileService.getById(id);
    }

    @Value("${sob.blog.image.max-size}")
    public long maxSize;

    /**
     * 图片上传
     * @param file    文件
     * @return
     */
    @ApiOperation(value = "图片上传")
    @PostMapping("/uploadImage")
    public ResponseResult uploadImage(MultipartFile file) {
        UploadImageBean uploadImageBean = null;
        try {
            uploadImageBean = new UploadImageBean();
            long size = file.getSize();
            // 限制文件的大小
            log.info("maxSize == > " + maxSize + "  size == > " + size);
            if (size > maxSize) {
                return ResponseResult.FAILED("图片最大支持" + (maxSize / 1024 / 1024) + "MB");
            }
            String month = DateUtils.dateToString(DateUtils.getNow(),"yyyy-MM");
            String path = "/headUrl/"+ month;
            log.info("文件上传地址：" + path);
            uploadImageBean.setPath(path);
            uploadImageBean.setQuality(0);
            UploadImageResult uploadImageResult = UploadImageUtil.uploadImageToCompressAndOriginal(file, uploadImageBean);
            uploadImageResult.setFileName(file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.SUCCESS("添加错误");
        }
        return ResponseResult.SUCCESS("添加成功").setData(uploadImageBean);
    }

    /**
     * 已发布分页
     *
     * @param current
     * @param limit
     * @param type
     * @return
     */
    @ApiOperation(value = "根据 类型 获取 荣誉 ")
    @GetMapping("/{current}/{limit}")
    public ResponseResult listArticle(@PathVariable Long current,
                                      @PathVariable Long limit,
                                      String type) {
        Page<IFile> page = new Page<>(current, limit);
        QueryWrapper<IFile> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(type)) {
            wrapper.eq("file_type", type);
        }

        wrapper.orderByDesc("create_time");
        Page<IFile> pageHospitalSet = iFileService.page(page, wrapper);
        return ResponseResult.SUCCESS("获取文章成功.")
                .setData(pageHospitalSet);
    }

}
